package com.mianyang.zhangsan.plugin.approval;

import com.mianyang.zhangsan.common.util.JsonUtils;
import com.mianyang.zhangsan.model.dto.SecApprovalDto;
import com.mianyang.zhangsan.model.entity.SecAccount;
import com.mianyang.zhangsan.model.entity.SecApproval;
import com.mianyang.zhangsan.model.entity.SecDept;
import com.mianyang.zhangsan.service.ISecAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 加入部门审批处理
 * @author jiangjr
 * @date 2022-10-25
 */
@Component
public class JoinDeptApproval extends AbstractApprovalHandler{
    @Autowired
    private ISecAccountService accountService;

    @Override
    public void handle(SecApprovalDto dto) {
        // 获取审批结果
        SecApproval approval = dto.getApproval();
        // 审批通过，修改用户部门信息
        if (approval.getApprovalStatus() == 1) {
            SecDept dept = JsonUtils.toBean(dto.getApply(), SecDept.class);
            SecAccount account = null;
            if (dept != null) {
                account = SecAccount.builder()
                        .accountId(approval.getSenderId())
                        .deptId(dept.getDeptId())
                        .deptName(dept.getDeptName()).build();
                // 更新用户信息
                accountService.updateById(account);
            }
        }
    }
}
